import QtQuick
import QtQuick.Controls
import QtQuick.Window
import QtQuick.Layouts

import QtQuickRO

Window{
    height:700
    width:800
    visible: true
    title:qsTr("服务端测试")

    Item{
        id:scene
        anchors.fill: parent
        Rectangle{
            id:hostScene
            border.width: 1
            width: parent.width / 2
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.left: parent.left
        }

        Rectangle{
            id:clientScene
            border.width: 1
            width: parent.width / 2
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.right: parent.right
        }
    }




    Component{
        id:rootComp
        Item{
            id:itemRoot
            anchors.fill: parent
            property int money:100
            property bool isRoot:true

            parent: {
                var role = this.SharedProperty.role
                if(role === ROSharedObject.Host)
                {
                    return hostScene
                }
                else if(role === ROSharedObject.Client)
                {
                    return clientScene;
                }

                return null
            }

            SharedProperty.properties:["money"]

            RORPC{
                id:rpc
                mode:RORPC.Fast
                policy:RORPC.CallOnServer
                function makeMoney(money){
                    console.log("makingMoney",location.text)
                    itemRoot.money += money
                }
            }

            Column{
                id:column
                anchors.fill: parent

                Text{
                    id:location
                    text: {
                        var role = exportProperty.role
                        if(role === ROSharedObject.Host)
                        {
                            return "服务端"
                        }
                        else if(role === ROSharedObject.Client)
                        {
                            return "客户端";
                        }

                        return null
                    }
                }

                Text{
                    text:{
                        if(exportProperty.role === ROSharedObject.Client)
                        {
                            return "玩家id" + node.playerId
                        }

                        return ""
                    }
                }

                Text{
                    text:{
                        return "钱数" + itemRoot.money
                    }
                }

                Button{
                    text:"button"
                    id:test

                    onClicked: {
                        rpc.call("makeMoney",[10])
                    }
                }

                Button{
                    text:"增加信息"
                    id:addMess

                    RORPC{
                        id:addMessRpc
                        mode:RORPC.Fast
                        policy:RORPC.CallOnServer
                        function addMessage(){
                            //ROHelper.createObjectFromComponent(messComp.comp)
                            messComp.spawnObject()
                        }
                    }

                    onClicked: {
                        addMessRpc.call("addMessage",[])
                    }
                }

                ROComponent{
                    id:messComp

                    comp: Button{
                        id:btn
                        parent:column
                        width: 100
                        height:30
                        text:"删除"
                        onClicked: {
                            btn.destroy()
                        }
                    }



                }

                Button{
                    text:"切换"
                    RORPC{
                        id:switchComp
                        mode:RORPC.Fast
                        policy:RORPC.CallOnServer
                        function switchComp(){
                            roloader.value = !roloader.value
                        }
                    }

                    onClicked: {
                        switchComp.call("switchComp")
                    }

                }

                ROLoader{
                    id:roloader
                    property bool value :true
                    sourceComponent: value?comp1:comp2
                }

                Component{
                    id:comp1
                    Text{
                        parent:column
                        text:"123"

                        Component.onDestruction: {
                            console.log("destroied",text)
                        }
                    }
                }

                Component{
                    id:comp2
                    Text{
                        parent:column
                        text:"456"

                        Component.onDestruction: {
                            console.log("destroied",text)
                        }
                    }
                }

            }



        }
    }

    ROHost{
        id:host
        rootComponent:rootComp

        port:2425

        policy:ROHost.ListenServer

        playerDelegate: ROPlayer{
            property string userName

            Component.onCompleted: {
                console.log("playerCreated",userName)
            }
        }

        hostPlayerParam:{
            "userName":"host"
        }

        Component.onCompleted: {
            active = true
        }
    }

    RONode{
        id:node

        host:"qtro://localhost:2425"

        rootComponent:rootComp
        loginParam: {
            "userName":"player"
        }

        Component.onCompleted: {
            active = true
        }

    }

}
